package com.monkeyk.sos.web.controller.portal.cooperator;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.monkeyk.sos.domain.dto.CooperDetailsDto;
import com.monkeyk.sos.domain.model.CooperDetails;
import com.monkeyk.sos.domain.shared.security.WdcyUserDetails;
import com.monkeyk.sos.service.CooperService;
import com.monkeyk.sos.util.WebUtils;
import com.monkeyk.sos.web.controller.BaseController;

/**
 * 
* @ClassName: CooperatorController 
* @Description: 合作者控制层 
* @author cygaod 
* @date 2015年12月10日 下午4:52:45 
* @version 1.0
 */
@Controller
@RequestMapping("/portal/cooperator")
public class CooperatorController extends BaseController {
	private static final Logger	Log	= Logger.getLogger(CooperatorController.class);
	
	@Autowired
	private CooperService		cooperService;
	
	@Autowired
	private   JavaMailSenderImpl javaMail;
	
	/**
	* @Title: MyDetails 
	* @Description: 合作者基本信息 
	* @param @param model
	* @param @param request
	* @param @param response
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping("/MyDetails")
	public String MyDetails(Model model, HttpServletRequest request, HttpServletResponse response) {
		Log.info("显示合作者基本信息 : " + "/portal/cooperator/MyDetails");
		UserDetails userDetails =  (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		model.addAttribute("cooperDetails", cooperService.findCooperDetailByCooperName(userDetails.getUsername()));
		return "portal/cooperator/basicInfor";
	}
	
	/**
	* @Title: updateMyDetails 
	* @Description: 更新合作者基本信息 
	* @param @param cooperDetails
	* @param @param request
	* @param @param response
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value="/updateMyDetails",method = RequestMethod.POST)
	public String updateMyDetails(CooperDetails cooperDetails, HttpServletRequest request, HttpServletResponse response) {
		Log.info("更新合作者基本信息 : " + "/portal/cooperator/updateMyDetails");
		cooperService.updateCooperDetail(cooperDetails);
		return "../../portalLogin";
	}
	
	/**
	* @Title: RestMyApp 
	* @Description: 合作者应用列表 
	* @param @param model
	* @param @param request
	* @param @param response
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping("/MyApp")
	public String RestMyApp(Model model, HttpServletRequest request, HttpServletResponse response) {
		Log.info("显示用户信息首页 : " + "/portal/cooperator/MyApp");
		String cooperName = ((UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
		CooperDetailsDto cooperDetailsDto = cooperService.getCooperDetailsDto(cooperName);
		model.addAttribute("cooperDetailsDto", cooperDetailsDto);
		return "portal/cooperator/appList";
	}
	
	
	
	/**
	* @Title: checkEmai 
	* @Description: 跳转验证邮箱页面 
	* @param @param manager
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value="/checkEmailPage")
	public String checkEmailPage(String email,Model model) {
	    Log.info("跳转验证邮箱页面  : " + "/checkEmailPage/");
	    model.addAttribute("email", email);
	    return "/portal/common/checkmail";
	}
	
	/**
	* @Title: checkEmail 
	* @Description: 发送邮件 
	* @param @param email
	* @param @param session
	* @param @param response    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@RequestMapping(value="/getEmailCode",method=RequestMethod.POST)
	public void checkEmail( String email,HttpSession session,HttpServletResponse response) {
	    Log.info("验证邮箱 : " + "/checkEmai/"+email);
	    Map<String, Object> map= new HashMap<String, Object>();
	    Map<String, String> result = WebUtils.sendEmaiCheck(email,javaMail);
	    String failMess = result.get("failMess");
	    if(failMess != ""){
	    	map.put("status", false);//邮件发送失败
	    	map.put("failMess", failMess);
	    }else{
	    	map.put("status", true);//邮件发送失败
	    	session.setAttribute("SEND_CODE_TIME", new Date().getTime());
		    session.setAttribute("CODE", result.get("code"));
		    Log.info("code" + result.get("code"));
	    };
	    writeJSON(response, map);
	}
	
	/**
	* @Title: checkCode 
	* @Description: 校验验证码 
	* @param @param code
	* @param @param session
	* @param @param response    设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@RequestMapping(value="/checkCode",method=RequestMethod.POST)
	public void checkCode(String code,HttpSession session,HttpServletResponse response) {
	    Log.info("验证验证码 : " + "/checkCode/"+code);
        WdcyUserDetails principal = (WdcyUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	    Map<String, Object> map= new HashMap<String, Object>();
	    Long time = 60L;
		Long codeTime = (Long) session.getAttribute("SEND_CODE_TIME");
		String codeLocal = (String) session.getAttribute("CODE");
	    if (codeTime != null) {
			time = (new Date().getTime() - codeTime) / 1000;
		}
		if (time >= 60) {
			map.put("status", "type1");//验证码过期
		} else {
			if(codeLocal.equalsIgnoreCase(code)){
				map.put("status", "type2");//验证码正确
				cooperService.updateStatus(1, String.valueOf(principal.getUser().getId()));
			}else {
				map.put("status", "type3");//验证码错误
			}
		}
		writeJSON(response, map);
	}
	
}
